package org.btik.light.mobile.ui.serial.controller;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import org.btik.light.mobile.R;
import org.btik.light.mobile.ui.serial.controller.entity.SerialPortInfo;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author lustre
 * @since 2023/4/6 18:37
 */
public class SerialPortAdapter extends ArrayAdapter<SerialPortInfo> {

    public SerialPortAdapter(@NonNull @NotNull Context context, int resource, @NonNull @NotNull List<SerialPortInfo> objects) {
        super(context, resource, objects);
    }

    public static int getItemId() {
        return R.layout.serial_list_item;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        SerialPortInfo serialPortInfo = getItem(position);
        if (convertView == null || (holder = (ViewHolder) convertView.getTag()) == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(getItemId(), parent, false);
            holder = new ViewHolder(convertView);
            holder.productName = convertView.findViewById(R.id.product_name);
            holder.devName = convertView.findViewById(R.id.dev_name);
            holder.manufacturerName = convertView.findViewById(R.id.manufacturer_name);
            holder.vendorName = convertView.findViewById(R.id.vendor_name);
            holder.vendorType = convertView.findViewById(R.id.vendor_type);
            convertView.setTag(holder);
        }

        holder.devName.setText(serialPortInfo.getDevName());
        holder.productName.setText(serialPortInfo.getProductName());
        holder.manufacturerName.setText(serialPortInfo.getManufacturerName());
        holder.vendorType.setText(serialPortInfo.getVendorType());
        holder.vendorName.setText(serialPortInfo.getVendorName());
        return convertView;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView devName;

        private TextView productName;

        private TextView manufacturerName;

        private TextView vendorName;

        private TextView vendorType;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }

    }

}
